import 'package:flutter/material.dart';

class FeedbackPage extends StatefulWidget {
  @override
  _FeedbackPageState createState() => _FeedbackPageState();
}

class _FeedbackPageState extends State<FeedbackPage> {
  String _selectedIssueType = "使用问题";
  final TextEditingController _feedbackController = TextEditingController();
  List<String> _uploadedImages = [];

  @override
  void dispose() {
    _feedbackController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("反馈页面"),
        leading: IconButton(
          icon: Icon(Icons.arrow_back),
          onPressed: () {
            Navigator.pop(context);
          },
        ),
      ),
      body: Padding(
        padding: const EdgeInsets.all(16.0),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Row(
              children: [
                Text("问题类型", style: TextStyle(fontSize: 16, fontWeight: FontWeight.bold)),
                Text("*", style: TextStyle(fontSize: 16, fontWeight: FontWeight.bold, color: Colors.red)),
              ],
            ),
            SizedBox(height: 8),
            Wrap(
              spacing: 8,
              runSpacing: 8,
              children: [
                _buildChip("使用问题"),
                _buildChip("账号问题"),
                _buildChip("功能建议"),
                _buildChip("内容建议"),
                _buildChip("投诉举报"),
                _buildChip("订单问题"),
              ],
            ),
            SizedBox(height: 16),
            Row(
              children: [
                Text("反馈内容", style: TextStyle(fontSize: 16, fontWeight: FontWeight.bold)),
                Text("*", style: TextStyle(fontSize: 16, fontWeight: FontWeight.bold, color: Colors.red)),
              ],
            ),
            SizedBox(height: 8),
            TextField(
              controller: _feedbackController,
              maxLines: 4,
              decoration: InputDecoration(
                border: OutlineInputBorder(),
                hintText: '请详细描述问题或者改进意见',
              ),
            ),
            SizedBox(height: 16),
            Text("上传图片(${_uploadedImages.length}/3)", style: TextStyle(fontSize: 16)),
            SizedBox(height: 8),
            IconButton(
              icon: Icon(Icons.add_a_photo),
              onPressed: () {
                // 上传图片逻辑，可扩展
                if (_uploadedImages.length < 3) {
                  setState(() {
                    _uploadedImages.add("image_path"); // 示例图片路径
                  });
                }
              },
            ),
            Spacer(),
            Center(
              child: ElevatedButton(
                onPressed: () {
                  _showConfirmationDialog();
                },
                child: Text("提交"),
                style: ElevatedButton.styleFrom(
                  minimumSize: Size(double.infinity, 48), // 设置按钮宽度为屏幕宽度
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }

  Widget _buildChip(String label) {
    return FilterChip(
      label: Text(label),
      selected: _selectedIssueType == label,
      onSelected: (bool selected) {
        setState(() {
          _selectedIssueType = label;
        });
      },
      selectedColor: Colors.blue,
    );
  }

  void _showConfirmationDialog() {
    showDialog(
      context: context,
      builder: (BuildContext context) {
        return AlertDialog(
          title: Text("确认提交"),
          content: Text("确认提交吗？"),
          actions: [
            TextButton(
              onPressed: () {
                Navigator.of(context).pop();
              },
              child: Text("取消"),
            ),
            TextButton(
              onPressed: () {
                Navigator.of(context).pop();
                _showSuccessMessage();
              },
              child: Text("确定"),
            ),
          ],
        );
      },
    );
  }

  void _showSuccessMessage() {
    ScaffoldMessenger.of(context).showSnackBar(
      SnackBar(
        content: Text("提交成功"),
        duration: Duration(seconds: 2),
      ),
    );
  }
}
